package calvin.study.rabbitmq.advanced.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import calvin.study.rabbitmq.base.RabbitMqBase;

public class Producer extends RabbitMqBase implements Runnable {

	public static void main(String[] args) throws Exception {
		Producer target = new Producer();

		for (int i = 0, len = 1; i < len; i++) {
			new Thread(target).start();
		}

	}

	@Override
	public void run() {
		for (int i = 0, len = 1000; i < len; i++) {
			try {
				aaa(i);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void aaa(int i) throws Exception {
		// 1 创建ConnectionFactory
		ConnectionFactory connectionFactory = getConnectionFactory();
		Connection connection = null;
		Channel channel = null;
		try {
			// 2 获取Connection
			connection = connectionFactory.newConnection();
			// 3 通过Connection创建一个新的Channel
			channel = connection.createChannel();
			// 4 指定我们的消息投递模式: 消息的确认模式
			//channel.confirmSelect();
			String exchangeName = "confirm_topic_exchange";
			String routingKey = "confirm.save";

			// 5 发送一条消息
			String msg = "消费端 限流策略测试数据：" + i;

			channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			channel.close();
			connection.close();
		}
	}
}
